package com.atguigu.exam.service.impl;

import com.atguigu.exam.properties.MinioProperties;
import com.atguigu.exam.service.FileUploadService;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

/**
 * projectName: com.atguigu.exam.service.impl
 *
 * @author: 赵伟风
 * description:
 */
@EnableConfigurationProperties(value = MinioProperties.class)
@Service
@Slf4j
public class FileUploadServiceImpl implements FileUploadService {
   @Autowired
    private MinioProperties minioproperties;

    @Operation
    public String uploadFile(MultipartFile file) {
        try {
            // 1.创建客户端对象
            MinioClient minioClient = MinioClient.builder()
                    //设置Minio服务器的地址
                    .endpoint(minioproperties.getEndpoint())
                    //设置用户名和密码
                    .credentials(minioproperties.getAccesskey(),minioproperties.getSecretkey())
                    .build();

            // 检查存储桶是否已经存在
            boolean isExist =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioproperties.getBucketname()).build());
            if(isExist) {
                System.out.println("Bucket "+minioproperties.getBucketname()+" already exists.");
            } else {
                //如果桶不存在则创建它
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioproperties.getBucketname()).build());
            }

            //获取文件名
            String originalFilename = file.getOriginalFilename();
            //获取上传的文件的后缀名
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //使用UUI随机生成一个字符串作为文件名
            String priffix = UUID.randomUUID().toString().replaceAll("-", "");
            //对当前日期进行格式化
            String format = DateFormatUtils.format(new Date(), "yyyy/MM/dd");
            //创建新的文件名
            String fileName = format+"/"+priffix + suffix;
            //获取输入流
            InputStream is = file.getInputStream();
            //获取文件的大小
            long size = file.getSize();

            //2.创建PutObjectArgs对象
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    //minio服务器中桶的名字
                    .bucket(minioproperties.getBucketname())
                    //设置上传到mimio服务器的文件名
                    .object(fileName)
                    //设置输入流
                    .stream(is,size,-1)
                    .build();

            //使用putObject上传文件到Minio服务器的桶中
            minioClient.putObject(putObjectArgs);
            return minioproperties.getEndpoint()+"/"+minioproperties.getBucketname()+"/"+fileName;
        } catch(Exception e) {
            System.out.println("Error occurred: " + e);
        }
        return null;
    }

    public static void main(String[] args) {
        String fileName = "ikun.gif";
        int index = fileName.lastIndexOf(".");
        System.out.println("index = " + index);
        //获取文件名的后缀
        String substring = fileName.substring(index);
        System.out.println("substring = " + substring);
        //使用UUID随机生成一个字符串作为文件名
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        System.out.println("uuid = " + uuid);
    }
}
